<?php


namespace App\Http\Controllers\admin\model;


use Illuminate\Support\Facades\DB;

class AdminAppModel
{
    public static function add($param){
        return DB::table('t_admin_app')->insert($param);
    }

    public static function delete($where){
        return DB::table('t_admin_app')->where($where)->delete();
    }

    public static function queryFirst($where,$field){
        return DB::table('t_admin_app')->where($where)->first($field);
    }

    public static function queryLeftFirst($where){
        $field = ['a.id','a.app_name','a.app_version_name','a.app_package','a.app_version_code','a.app_size','a.app_desc',
            'a.app_url','a.app_img','a.app_preview1','a.app_preview2','a.app_preview3','a.app_developer','a.app_downloads',
            'a.app_score','g.title','s.name','a.app_type','a.app_status','a.create_time','ad.username','a.top_search',
            'a.update_time','a.app_installs','a.app_md5','a.app_score','a.app_grade_id','a.app_subject_id','a.app_remarks'];
        return DB::table('t_admin_app as a')
            ->leftJoin('t_admin as ad','a.admin_id','=','ad.id')
            ->leftJoin('t_grade as g','a.app_grade_id','=','g.id')
            ->leftJoin('t_subject as s','a.app_subject_id','=','s.id')
            ->where($where)
            ->first($field);
    }

    public static function edit($where,$param){
        return DB::table('t_admin_app')->where($where)->update($param);
    }

    public static function queryCount($where=[],$param=[]){
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_admin_app as a')
                ->leftJoin('t_admin as ad','a.admin_id','=','ad.id')
                ->leftJoin('t_grade as g','a.app_grade_id','=','g.id')
                ->leftJoin('t_subject as s','a.app_subject_id','=','s.id')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->count('a.id');
        }
        return DB::table('t_admin_app as a')
            ->leftJoin('t_admin as ad','a.admin_id','=','ad.id')
            ->leftJoin('t_grade as g','a.app_grade_id','=','g.id')
            ->leftJoin('t_subject as s','a.app_subject_id','=','s.id')
            ->where($where)
            ->count('a.id');
    }

    public static function queryAll($where=[],$param=[],$page){
        $field = ['a.id','a.app_name','a.app_version_name','a.app_package','a.app_version_code','a.app_size','a.app_desc',
            'a.app_url','a.app_img','a.app_preview1','a.app_preview2','a.app_preview3','a.app_developer','a.app_downloads',
            'a.app_score','g.title','s.name','a.app_type','a.app_status','a.create_time','ad.username','a.top_search',
            'a.update_time','a.app_installs'];
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_admin_app as a')
                ->leftJoin('t_admin as ad','a.admin_id','=','ad.id')
                ->leftJoin('t_grade as g','a.app_grade_id','=','g.id')
                ->leftJoin('t_subject as s','a.app_subject_id','=','s.id')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->offset($page['startSize'])
                ->limit($page['pageSize'])
                ->get($field);
        }
        return DB::table('t_admin_app as a')
            ->leftJoin('t_admin as ad','a.admin_id','=','ad.id')
            ->leftJoin('t_grade as g','a.app_grade_id','=','g.id')
            ->leftJoin('t_subject as s','a.app_subject_id','=','s.id')
            ->where($where)
            ->offset($page['startSize'])
            ->limit($page['pageSize'])
            ->get($field);
    }
}